package com.anytron.service.impl;

import java.util.List;

import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerKey;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import com.anytron.config.timer.AppTimerTask;
import com.anytron.model.AppTimer;
import com.anytron.service.AppTimerService;
import com.anytron.service.QuartzManager;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class QuartzManagerImpl implements QuartzManager {
	
	@Autowired
	SchedulerFactoryBean schedulerFactoryBean;
	@Autowired
	AppTimerService appTimerService;

	@Override
	public void pushTimer(AppTimer appTimer,boolean isReStart){
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			CronTriggerImpl trigger = (CronTriggerImpl) scheduler.getTrigger(getTriggerKey(appTimer.getAppId()));
			if (trigger==null) {
				trigger = new CronTriggerImpl();
				JobDataMap jdm = new JobDataMap();
				jdm.put("appTimer", appTimer);
				JobDetailImpl jdi = new JobDetailImpl();
				jdi.setKey(getJobKey(appTimer.getAppId()));
				jdi.setJobDataMap(jdm);
				jdi.setDurability(true);
				jdi.setJobClass(AppTimerTask.class);
				trigger.setCronExpression(appTimer.getTimerCron());
				trigger.setKey(getTriggerKey(appTimer.getAppId()));
				trigger.setJobKey(getJobKey(appTimer.getAppId()));
				scheduler.addJob(jdi, true);
				scheduler.scheduleJob(trigger);
				if (isReStart) {
					log.info("调度任务:["+getAppSchedulerId(appTimer.getAppId())+"] 已重启");
				}else {
					log.info("调度任务:["+getAppSchedulerId(appTimer.getAppId())+"] 已启动");
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}

	@Override
	public boolean killTimer(Long appId) {
		try {
			JobKey jobKey = getJobKey(appId);
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			if (scheduler.getJobDetail(jobKey)!=null) {
				scheduler.deleteJob(jobKey);
				log.info("调度任务:["+jobKey.getName()+"] 已清除");
			} else {
				log.info("调度任务:["+jobKey.getName()+"] 不存在,无需清除");
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
		return true;
	}

	@Override
	public void startAllTimer() {
		List<AppTimer> list = appTimerService.list(new AppTimer());
		list.forEach(item->pushTimer(item,false));
	}

	@Override
	public boolean reStartTimer(Long appId) {
		boolean killTimer = killTimer(appId);
		if (killTimer) {
			AppTimer appTimer = new AppTimer();
			appTimer.setAppId(appId);
			AppTimer one = appTimerService.getOne(appTimer);
			if (one!=null) {
				pushTimer(one,true);
			}
			return true;
		}else {
			return false;
		}
	}
	
	private String getAppSchedulerId(Long appId) {
		return "scheduler_"+appId;
	}
	
	private JobKey getJobKey(Long appId) {
		return new JobKey(getAppSchedulerId(appId));
	}
	
	private TriggerKey getTriggerKey(Long appId) {
		return new TriggerKey(getAppSchedulerId(appId));
	}

}
